using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Sedna.Core.Controls.Support.Javascript;
using Sedna.Core.DataBinding;
using Sedna.Core.Tiles.Contents;
using DataBinder = Sedna.Core.DataBinding.DataBinder;

namespace Sedna.Core.Controls
{
    /// <summary>
    /// Hidden field to be used in BaseContainer.
    /// </summary>
    [ValidationProperty("Value")]
    public class Hidden : HtmlInputHidden, IContent, IConfigurationAware, IControlDataLoadAware, IClientScriptingControl
    {
        private IConfiguration configuration;
        private string configurationId = "configuration.default.Hidden";


        private bool clientScriptingEnabled;
        private string clientVariable;

        private String contentPlaceHolderId;

        private string javascriptClass;

        protected IDataBinder binder;

        #region Properties

        public String ContentPlaceHolderId
        {
            get { return contentPlaceHolderId; }
            set { contentPlaceHolderId = value; }
        }

        public bool ClientScriptingEnabled
        {
            get { return clientScriptingEnabled /*|| clientVariable != null*/; }
            set { clientScriptingEnabled = value; }
        }

        public string ClientVariable
        {
            get { return ControlUtil.GetClientVariable(this, clientVariable); }
            set { clientVariable = value; }
        }


        public IConfiguration Configuration
        {
            get { return configuration; }
            set { configuration = value; }
        }

        public string ConfigurationId
        {
            get { return configurationId; }
            set { configurationId = value; }
        }

        #endregion

        #region Javascript support

        public string JavascriptClass
        {
            get { return javascriptClass; }
            set { javascriptClass = value; }
        }

        public string NodeName
        {
            get { return ClientID; }
        }


        #endregion


        
        public Hidden()
        {
        }
        
        public Hidden(string id) : this()
        {
            this.ID = id;
        }

        public Hidden(IConfiguration configuration) : this()
        {
            this.configuration = configuration;
            this.configurationId = configuration.ObjectName;
            BuildControlFromConfiguration();
        }

        protected void BuildControlFromConfiguration()
        {
            this.ID = configuration.Id;
            IClientScriptingConfiguration clientScriptingConfiguration = Configuration as IClientScriptingConfiguration;
            if (clientScriptingConfiguration == null)
            {
                return;
            }

            ClientScriptingEnabled = clientScriptingConfiguration.ClientScriptingEnabled;
            ClientVariable = clientScriptingConfiguration.ClientVariable;
        }

        protected override void OnInit(EventArgs e)
        {
            if (configuration == null)
            {
                configuration = (IConfiguration) SpringContextFactory.GetContext().GetObject(configurationId);
            }
            Build();
            binder = new DataBinder(this, configuration, ExecutionContext.GetCurrent(ClientID));
            base.OnInit(e);
        }


        private void Build()
        {
//			this.ID = configuration.Id;
            Visible = configuration.IsVisibleForPrincipal();
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
            Unbind();
            ControlUtil.ExecuteOnLoad(executionContext, Context, Configuration, this.ClientID);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
            ControlUtil.ExecuteOnPreRender(executionContext, Context, Configuration, this.ClientID);
            Bind();
            if (ClientScriptingEnabled)
            {
                EnableClientScripting();
            }
        }

        public void OnDataLoad()
        {
            IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
            ControlUtil.ExecuteOnDataLoad(executionContext, Context, Configuration, this.ClientID);
        }

        private void EnableClientScripting()
        {
            ControlUtil.EnsureIdAttribute(this);
            JavascriptController controller = JavascriptController.Current;

            controller.DeclareClass("Hidden");
            controller.DeclareObjectWithQuotedParams(ClientVariable, "Hidden", ClientID);
        }

        private void Bind()
        {
            object value;
            if (binder.GetPropertyValue("Value", out value))
            {
                value = configuration.DataTranslator.Translate(this, value);
                Value = value != null ? value.ToString() : null;
            }
        }

        private void Unbind()
        {
            binder.Unbind("Value", configuration.DataParser);
        }
    }
}